package suanfa.lianxi;

import suanfa.datastructure.queue.Queue;

//9.如何创建一个队列，你能写一个程序模拟一下一边往队列中放入数据，一边访问队列吗？
public class Queue1 {
            node front,rear;
            int size=0;

            public Queue1(){
                this.front=null;
                this.rear=null;
                size=0;
            }
            public boolean isEmpty(){
                return front==null;
            }

            public void enqueue(int data){
                node newnode=new node(data);
                if(isEmpty()){
                    front=rear=newnode;
                }
                else{
                    rear.next=newnode;
                }
                rear=newnode;
                System.out.println("入队->"+data);
                size++;
            }
            public void fangwen(){
                node n=front;
                if(isEmpty()){
                    System.out.println("队列为空");
                    return;
                }
                while(n!=null){
                    System.out.print(n.data+"->");
                    n=n.next;
                }
                System.out.println("NULL");
            }

    public static void main(String[] args) {
        Queue1 q=new Queue1();
        for(int i=0;i<3;i++){
            q.enqueue((int)(Math.random()*10));
            q.fangwen();
        }


    }

            private static class node{
                public int data;
                node next;
                public node(int data){
                    this.data=data;
                }
            }
}
